package com.millstein.service.impl;

import com.millstein.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: tsing
 * @create: 2021-09-11 20:28
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.from}")
    private String from;

    /**
     * 发送邮件
     *
     * @param to      接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    @Override
    public void sendMail(String to, String subject, String content) {
        // 创建邮箱消息对象
        SimpleMailMessage message = new SimpleMailMessage();

        // 填充属性
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);

        javaMailSender.send(message);

        log.info("邮箱发送成功：{}", message.toString());
    }
}
